// const { getRounds } = require("bcrypt")

class Food{
  constructor({x=0,y=0,width=20,heigh=20,color='red'}={}){
      this.x = x
      this.y = y
      this.width = width
      this.heigh = heigh
      this.color = color
      this.elements = []
  }
  render(map){
    this.removefood(map)//删除之前的div
    this.addfood(map)//添加div
  }

  addfood(map){
    // console.log(Math.floor((Math.random() * map.offsetWidth ) + 1))
    this.x = Math.floor((Math.random() * map.offsetWidth) + 1);
    this.y = Math.floor((Math.random() * map.offsetHeight) + 1);
    let food = document.createElement('div') 
    map.appendChild(food)
    this.elements.push(food)
    // 设置food样式
    food.style.position = 'absolute'
    food.style.width = this.width +'px'
    food.style.height = this.heigh + 'px'
    food.style.top = this.y + 'px'
    food.style.left = this.x + 'px'
    food.style.backgroundColor = this.color
  }
  removefood(map){
    for(let i=this.elements.length-1;i>=0;i--){
      this.elements[i].parentNode.removeChild()
      this.elements.splice(i, 1)
    }
  }
}

export default Food